<?php
include("include/conn.php");
require('include/Smarty/libs/Smarty.class.php');

$party          = array();
$bhatthi        = array();
$bhatthiDate    = "";
$isEdit         = 0;
$bhatthiParty   = 0;
$weightPerc     = 2;
$bhatthiId      = (isset($_POST['party'])) ? $_POST['party'] : 0;
$rojmelEntry    = "";
$bhatthiBookId  = isset($_POST['bhatthiId']) ? $_POST['bhatthiId'] : "";
$requiredRojmelId = 0;
$prevFine = 0;
$prevAmount = 0;

if(isset($_POST['bhatthiAddBtn']) && isset($_POST['party']))
{
	if($_POST['isEdit'] = '1')
  {
  	if(isset($_POST['bhatthiId']) && $_POST['bhatthiId'] > 0)
	  {
	     $updateBhatthiBook = "UPDATE bhatthibook 
	                             SET bhatthiPartyId = ".$_POST['party'].", bhatthiDate = '".$_POST['curDateYear']."-".$_POST['curDateMonth']."-".$_POST['curDateDay']."',
	                                 prevFine = ".$_POST['partyFine'].",prevAmount = ".$_POST['partyAmount']."
	                           WHERE bhatthiBookId = ".$bhatthiBookId;
	    $updateBhatthiBookRes = mysql_query($updateBhatthiBook);
	    
	    $updateRojmelId = "SELECT rojmelId
                           FROM bhatthibookdetail 
                          WHERE bhatthiBookId = ".$bhatthiBookId;
      $updateRojmelIdRes = mysql_query($updateRojmelId);   
	    while($idRow = mysql_fetch_array($updateRojmelIdRes))
	    {
	    	$deleteRojmelQuery = "DELETE FROM rojmel
		                          WHERE rojmelId = ".$idRow['rojmelId'];
		    $deleteRojmelQueryRes = mysql_query($deleteRojmelQuery);
	    }
	    $deleteBhatthiDetail = "DELETE FROM bhatthibookdetail 
	                             WHERE bhatthiBookId = ".$bhatthiBookId;
	    $deleteBhatthiDetailRes = mysql_query($deleteBhatthiDetail);
	  }
	  else
	  {
	  	 $bhatthiBook = "INSERT INTO bhatthibook(bhatthiPartyId,bhatthiDate,prevFine,prevAmount)
	                    VALUES (".$_POST['party'].",'".$_POST['curDateYear']."-".$_POST['curDateMonth']."-".$_POST['curDateDay']."',".$_POST['partyFine'].",
	                            ".$_POST['partyAmount'].")";
	    $bhatthiBookRes = mysql_query($bhatthiBook);
	    $bhatthiBookId = mysql_insert_id();
	  }
    $loopCount = 0;
    while($loopCount < count($_POST['particulars']))
    {
      $particulars = ($_POST['particulars'][$loopCount] != '') ? $_POST['particulars'][$loopCount] : "";
      $grossWeight = ($_POST['grossWeight'][$loopCount] != '') ? $_POST['grossWeight'][$loopCount] : 0;
      $weightPerc  = ($_POST['weightPerc'][$loopCount] != '') ? $_POST['weightPerc'][$loopCount] : 0;
      $weight      = ($_POST['weight'][$loopCount] != '') ? $_POST['weight'][$loopCount] : 0;
      $touch       = ($_POST['touch'][$loopCount] != '') ? $_POST['touch'][$loopCount] : 0;
      $fine        = ($_POST['fine'][$loopCount] != '') ? $_POST['fine'][$loopCount] : 0;
      $qty         = ($_POST['qty'][$loopCount] != '') ? $_POST['qty'][$loopCount] : 0;
      $rate        = ($_POST['rate'][$loopCount] != '') ? $_POST['rate'][$loopCount] : 0;
      $amount      = ($_POST['amount'][$loopCount] != '') ? $_POST['amount'][$loopCount] : 0;
      
      if($_POST['particulars'][$loopCount] != '' || $_POST['weight'][$loopCount] != '' || $_POST['touch'][$loopCount] != '' || $_POST['fine'][$loopCount] != '' || $_POST['amount'][$loopCount] != '')
      {
      	$requiredRojmelId = 0;
        // Insert where checkbox selected :start 
	      if(isset($_POST['rojmelEntry'][$loopCount]))
	      { 
	        $getPartyName = "SELECT bhatthiPartyId,bhatthipartyName 
	                           FROM bhatthiparty
	                          WHERE bhatthiPartyId =".$_POST['party'];
	        $getPartyNameRes = mysql_query($getPartyName);
	        while($partyNameRow = mysql_fetch_array($getPartyNameRes))
			    {
			      $partyName      = $partyNameRow['bhatthipartyName'];
			      $bhatthiPartyId = $partyNameRow['bhatthiPartyId'];
			    }
			      
	        $insertRoamel = "INSERT INTO rojmel(rojmelDate, bhatthiPartyRojmelId, partyName, particulars, fine, amount, CrDr)
	                         VALUES ('".$_POST['curDateYear']."-".$_POST['curDateMonth']."-".$_POST['curDateDay']."','".$bhatthiPartyId."',
	                                 '".$partyName."','".$particulars."',".$fine.",".$amount.",'Dr')";
	        $insertRoamelRes = mysql_Query($insertRoamel);
	        $requiredRojmelId = mysql_insert_id();
	      }// Insert where checkbox selected :start
	      
        $bhatthiBookDetail = "INSERT INTO bhatthibookdetail(bhatthiBookId,particulars,grossWeight,weightPerc,weight,touch,fine,qty,rate,amount,CrDr,rojmelId)
                              VALUES (".$bhatthiBookId.",'".$particulars."',".$weight.",".$grossWeight.",".$weightPerc.",".$touch.",".$fine.",".$qty.",".$rate.",".$amount.",'Dr',".$requiredRojmelId.")";
        $bhatthiBookDetailRes = mysql_query($bhatthiBookDetail);
      }
      $loopCount++;
	  }
	}
	header("location:bhatthiDetail.php?bhatthiId=".$bhatthiId);
}
if(isset($_GET['bhatthiBookId']) && $_GET['bhatthiBookId'] > 0 )
{
	$isEdit = 1;
  $selectDate = "SELECT bhatthiBookId,bhatthiPartyId,bhatthiDate,prevFine,prevAmount FROM bhatthibook
                  WHERE bhatthiBookId = ".$_GET['bhatthiBookId'];
  $selectDateRes = mysql_query($selectDate);
  $row = mysql_fetch_assoc($selectDateRes);
  $bhatthiDate  = $row['bhatthiDate'];
  $bhatthiParty = $row['bhatthiPartyId'];
  $bhatthiId    = $row['bhatthiBookId'];
  $prevFine     = $row['prevFine'];
  $prevAmount   = $row['prevAmount'];
  
  $selectBhatthi = "SELECT bhatthiBookId, particulars, weight,grossWeight, touch, fine,qty,rate,amount, rojmelId
                      FROM bhatthibookdetail
                     WHERE bhatthiBookId = ".$_GET['bhatthiBookId'];
  $selectBhatthiRes = mysql_query($selectBhatthi);
  $a = 0;
  while($bhatthiRow = mysql_fetch_assoc($selectBhatthiRes))
  {
    $bhatthi[$a]['bhatthiBookId'] = $bhatthiRow['bhatthiBookId'];
    $bhatthi[$a]['particulars']   = $bhatthiRow['particulars'];
    $bhatthi[$a]['grossWeight']   = $bhatthiRow['grossWeight'];
    $bhatthi[$a]['weight']        = $bhatthiRow['weight'];
    $bhatthi[$a]['touch']         = $bhatthiRow['touch'];
    $bhatthi[$a]['fine']          = $bhatthiRow['fine'];
    $bhatthi[$a]['qty']           = $bhatthiRow['qty'];
    $bhatthi[$a]['rate']          = $bhatthiRow['rate'];
    $bhatthi[$a]['amount']        = $bhatthiRow['amount'];
    $bhatthi[$a]['rojmelId']      = $bhatthiRow['rojmelId'];
    $a++;
  }
}

// this for select bhatthi party : Start
$selectBhatthiParty = "SELECT bhatthiPartyId,bhatthipartyName 
                         FROM bhatthiparty
                        ORDER BY bhatthipartyName DESC";
$selectBhatthiPartyRes = mysql_query($selectBhatthiParty);
$a = 0; 
$party['id'][$a]   = 0;
$party['name'][$a] = "Select";
$a++;
while($partyRow = mysql_fetch_assoc($selectBhatthiPartyRes))
{
  $party['id'][$a]   = $partyRow['bhatthiPartyId'];
  $party['name'][$a] = $partyRow['bhatthipartyName'];
  $a++;
}
// this for select bhatthi party : End

$smarty = new Smarty;
require("bottom.php");
$smarty->assign("l_bhatthiCredit",$l_bhatthiCredit);
$smarty->assign("l_partySelect",$l_partySelect);
$smarty->assign("l_date",$l_date);
$smarty->assign("l_particulars",$l_particulars);
$smarty->assign("l_grossWeight",$l_grossWeight);
$smarty->assign("weightPerc",$weightPerc);
$smarty->assign("l_weight",$l_weight);
$smarty->assign("l_touch",$l_touch);
$smarty->assign("l_fine",$l_fine);
$smarty->assign("l_qty",$l_qty);
$smarty->assign("l_rate",$l_rate);
$smarty->assign("l_amount",$l_amount);
$smarty->assign("l_add",$l_add);
$smarty->assign("l_remove",$l_remove);
$smarty->assign("l_total",$l_total);
$smarty->assign("l_add",$l_add);
$smarty->assign("l_save",$l_save);

$smarty->assign("party",$party);
$smarty->assign("bhatthiDate",$bhatthiDate);
$smarty->assign("bhatthiParty",$bhatthiParty);
$smarty->assign("isEdit",$isEdit);
$smarty->assign("bhatthi",$bhatthi);
$smarty->assign("bhatthiId",$bhatthiId);
$smarty->assign("rojmelEntry",$rojmelEntry);
$smarty->assign("requiredRojmelId",$requiredRojmelId);
$smarty->assign("prevFine",$prevFine);
$smarty->assign("prevAmount",$prevAmount);
$smarty->display("bhatthiDebitEntry.tpl");
?>